package com.example.controller;

import com.example.entity.Dept;
import com.example.entity.Employee;
import com.example.entity.Job;
import com.example.entity.example.DeptExample;
import com.example.entity.example.EmployeeExample;
import com.example.entity.example.JobExample;
import com.example.entity.vo.EmployeeVO;
import com.example.service.DeptService;
import com.example.service.EmployeeService;
import com.example.service.JobService;
import com.example.util.ResultEntity;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@PreAuthorize("hasRole('ROLE_2') or hasRole('ROLE_3') ")
@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private JobService jobService;

    private EmployeeVO transformToEmployeeVO(Employee employee) {
        EmployeeVO employeeVO = new EmployeeVO();
        BeanUtils.copyProperties(employee, employeeVO);
        String deptName = deptService.getNameById(employee.getDeptId());
        String jobName = jobService.getNameById(employee.getJobId());
        String sex = employee.getSex();
        if ("1".equals(sex)) {
            sex = "男";
        } else if ("2".equals(sex)) {
            sex = "女";
        }
        employeeVO.setDeptName(deptName);
        employeeVO.setJobName(jobName);
        employeeVO.setSex(sex);
        return employeeVO;
    }

    @ResponseBody
    @RequestMapping("/get/all/employees.json")
    public ResultEntity<List<EmployeeVO>> getAllEmployees(@RequestParam("page") Integer page,
                                                          @RequestParam("limit") Integer limit,
                                                          @RequestParam(value = "id", required = false) Integer id,
                                                          @RequestParam(value = "did", required = false) Integer did,
                                                          @RequestParam(value = "jid", required = false) Integer jid,
                                                          @RequestParam(value = "phone", required = false) String phone,
                                                          @RequestParam(value = "name", required = false) String name
    ) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (did != null) {
            criteria.andDeptIdEqualTo(did);
        }
        if (jid != null) {
            criteria.andJobIdEqualTo(jid);
        }
        if (phone != null && !phone.isEmpty()) {
            criteria.andPhoneLike("%" + phone + "%");
        }
        if (name != null && !name.isEmpty()) {
            criteria.andNameEqualTo("%" + name + "%");
        }
        List<Employee> allEmployees = employeeService.getByExampleWithLimit(example, page, limit);
        List<EmployeeVO> allEmployeeVOs = new ArrayList<>(allEmployees.size());
        for (Employee employee : allEmployees) {
            EmployeeVO employeeVO = transformToEmployeeVO(employee);
            allEmployeeVOs.add(employeeVO);
        }
        int count = (int) employeeService.getCountByExample(example);
        return ResultEntity.createResultEntity(0, null, count, allEmployeeVOs);
    }

    @RequestMapping("/add/page.html")
    public String addPage(ModelMap modelMap) {
        List<Dept> deptList = deptService.getByExampleWithLimit(new DeptExample(), 1, Integer.MAX_VALUE);
        List<Job> jobList = jobService.getByExampleWithLimit(new JobExample(), 1, Integer.MAX_VALUE);
        modelMap.addAttribute("deptList", deptList);
        modelMap.addAttribute("jobList", jobList);
        return "include/add-employee";
    }

    @RequestMapping("/{id}/modify/page.html")
    public String modifyPage(ModelMap modelMap, @PathVariable("id") Integer id) {
        List<Dept> deptList = deptService.getByExampleWithLimit(new DeptExample(), 1, Integer.MAX_VALUE);
        List<Job> jobList = jobService.getByExampleWithLimit(new JobExample(), 1, Integer.MAX_VALUE);
        Employee employee = employeeService.getById(id);
        modelMap.addAttribute("deptList", deptList);
        modelMap.addAttribute("jobList", jobList);
        modelMap.addAttribute("employee", employee);
        return "include/modify-employee";
    }


    @ResponseBody
    @RequestMapping("/add")
    public ResultEntity<Object> addEmployee(@RequestBody Employee employee) {
        employee.setCreateDate(new Date());
        try {
            employeeService.addEmployee(employee);
        } catch (Exception e) {
            return ResultEntity.createResultEntity(-1, e.getMessage(), 0, null);
        }
        return ResultEntity.createResultEntity(0, null, 0, null);
    }

    @ResponseBody
    @RequestMapping("/remove")
    public ResultEntity<Object> removeById(@Param("id") Integer id) {
        try {
            employeeService.removeById(id);
        } catch (Exception e) {
            return ResultEntity.createResultEntity(-1, e.getMessage(), 0, null);
        }
        return ResultEntity.createResultEntity(0, null, 0, null);
    }

    @ResponseBody
    @RequestMapping("/update")
    public ResultEntity<Object> updateSelectiveById(@RequestBody Employee employee) {
        try {
            employeeService.updateSelectiveById(employee);
        } catch (Exception e) {
            return ResultEntity.createResultEntity(-1, e.getMessage(), 0, null);
        }
        return ResultEntity.createResultEntity(0, null, 0, null);
    }
}
